package close.kenny.shell.view
{	
	import close.kenny.lib.kwindows.appmenu.events.KAppMenuEvent;
	import close.kenny.lib.kwindows.window.KWindow;
	import close.kenny.shell.ShellFacade;
	import close.kenny.shell.view.ui.Desktop;
	
	import flash.events.MouseEvent;
	
	import mx.collections.XMLListCollection;
	import mx.controls.Alert;
	import mx.core.IVisualElement;
	import mx.core.IVisualElementContainer;
	import mx.events.DragEvent;
	import mx.events.ModuleEvent;
	import mx.modules.IModuleInfo;
	import mx.modules.ModuleManager;
	import mx.utils.ArrayUtil;
	
	import org.puremvc.as3.multicore.interfaces.IMediator;
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	
	import spark.components.Button;
	import spark.components.Image;

	public class DesktopMediator extends Mediator implements IMediator
	{
		public static const NAME:String="DesktopMediator";

		public function DesktopMediator(viewComponent:Desktop)
		{
			super(NAME, viewComponent);
		}

		private function get desktop():Desktop
		{
			return viewComponent as Desktop;
		}

		override public function onRegister():void
		{
			desktop.appMenu.addEventListener(KAppMenuEvent.ITEMCLICK,appMenuHandle);
		}
		override public function onRemove():void
		{
			desktop.appMenu.removeEventListener(KAppMenuEvent.ITEMCLICK,appMenuHandle);		}
		override public function listNotificationInterests():Array
		{
			return [ShellFacade.APPMENUINI,];
		}
		
		override public function handleNotification(note:INotification):void
		{
			switch (note.getName())
			{
				case ShellFacade.APPMENUINI:
					var applist:XMLListCollection=note.getBody() as XMLListCollection;
					desktop.appMenu.appMenuDataProvider=applist;
					var applist2:XMLList=new XMLList(applist.getItemAt(0));
					desktop.appMenu.appMenuDataProvider2=new XMLListCollection(applist2.children());
					break;
			}
		}
		public function appMenuHandle(event:KAppMenuEvent):void
		{
			var win:KWindow=new KWindow();
			
			win.title=desktop.appMenu.branchMenu.selectedItem.@label;
			win.name=desktop.appMenu.branchMenu.selectedItem.@url;
			if(desktop.windowsContainer.visible==true)
			{
				sendNotification(ShellFacade.ADDWINDOW,win);
			}
			if(desktop.rwindowsContainer.visible==true)
			{
				sendNotification(ShellFacade.ADDWINDOWRIGHT,win);
			}
			if(desktop.lwindowsContainer.visible==true)
			{
				sendNotification(ShellFacade.ADDWINDOWLEFT,win);
			}
		}
	}
}